package com.javacv.plus.extensions.image.steps;

import com.javacv.plus.core.processor.ProcessorContext;
import com.javacv.plus.core.processor.ProcessorStep;
import com.javacv.plus.extensions.image.ImageProcessor;
import com.javacv.plus.extensions.image.ProcessorResult;
import lombok.extern.slf4j.Slf4j;
import org.bytedeco.opencv.opencv_core.Mat;
import org.bytedeco.opencv.opencv_core.MatVector;

import static org.bytedeco.opencv.global.opencv_core.*;
import static org.bytedeco.opencv.global.opencv_imgproc.*;

/**
 * 颜色调整处理步骤
 * 支持基本的颜色调整功能
 */
@Slf4j
public class ColorAdjustStep implements ProcessorStep<ImageProcessor, ProcessorResult> {
    
    public enum AdjustType {
        HISTOGRAM_EQUALIZATION("直方图均衡化");
        
        private final String description;
        
        AdjustType(String description) {
            this.description = description;
        }
        
        public String getDescription() {
            return description;
        }
    }
    
    private final AdjustType adjustType;
    private final double[] parameters;
    
    /**
     * 创建颜色调整步骤
     * @param adjustType 调整类型
     * @param parameters 调整参数
     */
    public ColorAdjustStep(AdjustType adjustType, double... parameters) {
        this.adjustType = adjustType;
        this.parameters = parameters;
    }
    
    @Override
    public ProcessorResult execute(ImageProcessor processor, ProcessorContext context) throws Exception {
        Mat currentImage = context.getAttribute("currentImage");
        if (currentImage == null || currentImage.empty()) {
            throw new IllegalStateException("当前图像为空，无法进行颜色调整");
        }
        
        Mat adjustedImage = new Mat();
        
        try {
            switch (adjustType) {
                case HISTOGRAM_EQUALIZATION:
                    equalizeHistogram(currentImage, adjustedImage);
                    break;
                default:
                    throw new UnsupportedOperationException("不支持的颜色调整类型: " + adjustType);
            }
            
            context.setAttribute("currentImage", adjustedImage);
            
            log.info("颜色调整完成: {}", adjustType.getDescription());
            return null;
            
        } catch (Exception e) {
            if (adjustedImage != null) {
                adjustedImage.release();
            }
            throw new RuntimeException("颜色调整失败: " + adjustType.getDescription(), e);
        }
    }
    
    /**
     * 直方图均衡化
     */
    private void equalizeHistogram(Mat src, Mat dst) {
        if (src.channels() == 1) {
            // 灰度图像直接均衡化
            equalizeHist(src, dst);
        } else {
            // 彩色图像转到Lab色彩空间，只均衡化亮度通道
            Mat lab = new Mat();
            cvtColor(src, lab, COLOR_BGR2Lab);
            
            MatVector channels = new MatVector(3);
            split(lab, channels);
            
            // 均衡化L通道（亮度）
            equalizeHist(channels.get(0), channels.get(0));
            
            merge(channels, lab);
            cvtColor(lab, dst, COLOR_Lab2BGR);
            
            lab.release();
            channels.close();
        }
    }
    
    @Override
    public String getStepName() {
        return "ColorAdjust-" + adjustType.name();
    }
    
    @Override
    public int getPriority() {
        return 65; // 在滤镜之后，保存之前
    }
} 